package com.ibuonline.b2b.member.action;

import java.sql.Timestamp;
import java.util.List;

import org.apache.log4j.Logger;

import com.dimeng.common.base.BaseAction;
import com.dimeng.common.model.PageBean;
import com.dimeng.common.util.StringUtil;
import com.dimeng.common.util.SystemConfig;
import com.ibuonline.b2b.common.Constants;
import com.ibuonline.b2b.entity.Inform;
import com.ibuonline.b2b.entity.Member;
import com.ibuonline.b2b.member.service.InformService;
import com.ibuonline.b2b.member.service.MemberService;

/**
 * 
 * @author lianglong
 * @date Jul 7, 2011
 * @time 2:41:18 PM
 * @version V 1.0 $Id: InformAction.java 5973 2011-09-14 08:04:59Z lianglong $
 */
public class InformAction extends BaseAction {
	
	private static final long serialVersionUID = 5202988161410018787L;
	private static final Logger logger = Logger.getLogger(InformAction.class);
	private InformService informService;
	private MemberService memberService;
	
	private Inform inform;
	private List<Inform> informs;

	private String informIds;
	private PageBean pageBean;
	
	/**
	 * to add link Inform
	 * @return
	 */
	public String toAddLinkInform(){
		String path = inform.getLink();
		path = path.substring(path.indexOf("'")).replace("'", "").replace(")", "");
		inform.setLink(SystemConfig.getProperty("app.url") + path);
		return SUCCESS;
	}
	/**
	 * to add Msg Inform
	 * @return
	 */
	public String toAddMsgInform(){
		return SUCCESS;
	}
	/**
	 * add link inform
	 * @return
	 */
	public String addInform() {
		if (StringUtil.isEmpty(inform.getLink()) || StringUtil.isEmpty(inform.getContent())) 
			return INPUT;
		
		if(inform.getLink().length() < 100 && inform.getContent().length() < 500){
			Timestamp now = new Timestamp(System.currentTimeMillis());
			inform.setContent(StringUtil.clearScriptCode(inform.getContent()));
			inform.setLink(StringUtil.clearScriptCode(inform.getLink()));
			inform.setType(Constants.INFORM_TYPE_INFORM);
			inform.setCreateTime(now);
			inform.setLastupdate(now);
			inform.setStatus(Constants.INFORM_STATUS_NOT_PROCESSING);
			informService.save(inform);
			logger.info("add link inform successful");
		}
		
		return toLinkInformsList();
	}
	
	/**
	 * add msg inform
	 * @return
	 */
	public String addMsgInform() {
		if (StringUtil.isEmpty(inform.getDefendant()) || StringUtil.isEmpty(inform.getContent())) 
			return INPUT;
		
		Member memberObj = null;
		if (!inform.getDefendant().contains("@")) {
		    memberObj = memberService.getMemberByLoginName(inform.getDefendant());
		} else {
			memberObj = memberService.getMemberByEmail(inform.getDefendant());
		}
		
		if (memberObj != null) {
			if(inform.getDefendant().length() < 100 &&
					inform.getContent().length() < 500 &&
					inform.getReference().length() < 500) {
				Timestamp now = new Timestamp(System.currentTimeMillis());
				inform.setContent(StringUtil.clearScriptCode(inform.getContent()));
				inform.setDefendant(StringUtil.clearScriptCode(inform.getDefendant()));
				inform.setReference(StringUtil.clearScriptCode(inform.getReference()));
				inform.setType(Constants.INFORM_TYPE_MESSAGE);
				inform.setCreateTime(now);
				inform.setLastupdate(now);
				inform.setStatus(Constants.INFORM_STATUS_NOT_PROCESSING);
				informService.save(inform);
				logger.info("add msg inform successful");
			}
		} else {
			setMessage(getText("auth.error.account.not.exist"));
			return INPUT;
		}
		
		return toLinkInformsList();
	}
	
	/**
	 * to link inform list page
	 * @return
	 */
	public String toLinkInformsList() {
		Member member = (Member)this.getCurrentMember();
		if (StringUtil.isEmpty(inform.getType()))
			return INPUT;
		if (pageBean == null || pageBean.getAllRow() == 0) {
			int rows = informService.getRecordCount(member.getMemberId(), inform.getType(), inform.getStatus());
			pageBean = new PageBean(rows, 10, 1);
		} else pageBean.init();
		informs = informService.getInforsListByType(member.getMemberId(), inform.getType(), inform.getStatus(), pageBean);
		return SUCCESS;
	}
	
	/**
	 * to msg inform list page
	 * @return
	 */
	public String toMsgInformsList() {
		Member member = (Member)this.getCurrentMember();
		if (StringUtil.isEmpty(inform.getType()))
			return INPUT;
		
		if (pageBean == null || pageBean.getAllRow() == 0) {
			int rows = informService.getRecordCount(member.getMemberId(), inform.getType(), inform.getStatus());
			pageBean = new PageBean(rows, 10, 1);
		} else pageBean.init();
		pageBean.setOneNavigation(true);
		informs = informService.getInforsListByType(member.getMemberId(), inform.getType(), inform.getStatus(), pageBean);
		return SUCCESS;
	}
	
	/**
	 * to link inform details page
	 * @return
	 */
	public String toLinkInformDetails() {
		if (inform.getId() == null)
			return INPUT;
		inform = informService.get(inform.getId());
		return SUCCESS;
	}
	
	/**
	 * to msg inform details page
	 * @return
	 */
	public String toMsgInformDetails() {
		if (inform.getId() == null)
			return INPUT;
		inform = informService.get(inform.getId());
		return SUCCESS;
	}
	
	public String delLinkInforById() {
		if(inform == null || inform.getId() == null)
			return INPUT;
		Inform informObj = informService.get(inform.getId());
		if (informObj != null) {
			inform = informObj;
			informService.delete(informObj);
		}
		return toLinkInformsList();
	}
	
	/**
	 * delete inform by ids
	 * @return
	 */
	public String delLinkInforsByIds() {
		if(StringUtil.notEmpty(informIds)) {
			String[] ids = informIds.split(",");
			if(ids.length > 0){
				for (String id : ids) {
					Inform informObj = informService.get(new Integer(id));
					if(informObj != null) {
						inform = informObj;
						informService.delete(informObj);
					}
				}
			}
		}
		return toLinkInformsList();
	}
	
	public String delMsgInforById() {
		if(inform == null || inform.getId() == null)
			return INPUT;
		Inform informObj = informService.get(inform.getId());
		if (informObj != null) {
			inform = informObj;
			informService.delete(informObj);
		}
		return toMsgInformsList();
	}
	
	/**
	 * delete inform by ids
	 * @return
	 */
	public String delMsgInforsByIds() {
		if(StringUtil.notEmpty(informIds)) {
			String[] ids = informIds.split(",");
			if(ids.length > 0){
				for (String id : ids) {
					Inform informObj = informService.get(new Integer(id));
					if(informObj != null) {
						inform = informObj;
						informService.delete(informObj);
					}
				}
			}
		}
		return toMsgInformsList();
	}
	
	public Inform getInform() {
		return inform;
	}

	public void setInform(Inform inform) {
		this.inform = inform;
	}

	public List<Inform> getInforms() {
		return informs;
	}

	public void setInforms(List<Inform> informs) {
		this.informs = informs;
	}

	public void setInformService(InformService informService) {
		this.informService = informService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public String getInformIds() {
		return informIds;
	}

	public void setInformIds(String informIds) {
		this.informIds = informIds;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	
}
